<?php
namespace app\manage\controller;
obj('api/Api')->isSession('manage_system','index.php?r=manage/login/index');
class  itemsController extends \app\base\controller\BaseController
{
    public function index()
    {
    	$this->pagetext=array("专辑管理","专辑列表");
    	$where[] = "1";
    	$baseurl = "index.php?r=manage/items/index";
        $Page = obj('api/ApiData')->page("50", "forum", $where, "`id` DESC", $baseurl);
        $this->Page = $Page;
        $this->display();
    }
   
    public function additems(){

    	if(!IS_POST){
    		$this->pagetext=array("专辑管理","创建专辑");
    		
    		//加载马甲号
    		$vestlist=obj("index/global","controller")->vestlist("y");
    		$this->vestlist=$vestlist;

    		//加载分类
            $type=obj("index/global","controller")->type("y","null","group");
            $this->type=$type;
    		$this->display();
    		exit;
    	}else{
    		 $title=$this->arg("title");
    		 $pic=$this->arg("pic");
    		 $type=$this->arg("type");
    		 $keywords=$this->arg("keywords");
    		 $dec=$this->arg("dec");
    		 $uid=$this->arg("uid");
    		 self::CheckItemsForm($title,$type,$keywords,$dec,$pic,$uid);
    		 $data['uid']=$uid;
		     $data['title']=$title;
		     $data['keywords']=$keywords;
		     $data['dec']=$dec;
		     $data['pic']=$pic;
		     $data['groupid']=$type;
		     $data['date']=date("Y-m-d H:i:s",time());
		     obj("api/Apidata")->Inserts("forum",$data);
		     exit(json_encode(array("info" => "保存成功", "status" => "y")));
    	}
    }

    public function editoritems(){
    	if(!IS_POST){
    	  $this->pagetext=array("专辑管理","编辑专辑");
    	  //加载马甲号
    	  $vestlist=obj("index/global","controller")->vestlist("y");
    	  $this->vestlist=$vestlist;

    	  //加载分类
          $type=obj("index/global","controller")->type("y","null");
          $this->type=$type;

          $id=$this->arg("id");
          $id=$this->arg("id");
          $where[]="  `id` ={$id} ";
          $ret=obj("api/Apidata")->Data_Select("forum",$where);
          $this->ret=$ret;
          $this->html='<input type="hidden" name="id" value="'.$ret['id'].'" />';
          $this->display('app/manage/view/items/additems');
    	  exit;
    	}else{
    		 $title=$this->arg("title");
    		 $pic=$this->arg("pic");
    		 $type=$this->arg("type");
    		 $keywords=$this->arg("keywords");
    		 $dec=$this->arg("dec");
    		 $uid=$this->arg("uid");
    		 self::CheckItemsForm($title,$type,$keywords,$dec,$pic,$uid);
    		 $data['uid']=$uid;
		     $data['title']=$title;
		     $data['keywords']=$keywords;
		     $data['dec']=$dec;
		     $data['pic']=$pic;
		     $data['groupid']=$type;
		     $id=$this->arg("id");
             $where[]="  `id` ={$id} ";
             obj("api/Apidata")->Data_Updata("forum",$data,$where);
             exit(json_encode(array("info" => "保存成功", "status" => "y")));   

    	}
    }

    public function deleteitems(){

        error_reporting('0');
        $id=$this->arg("id");

        //删除专辑
       $where = " `id` ={$id}";
       obj('api/ApiData')->Deletethis('forum', $where);

        //删除商品
        $where_c="`fid` ={$id}";
        obj('api/ApiData')->Deletethis('items', $where_c);

        //删除评论
        $where_c="`aid` ={$id}";
        obj('api/ApiData')->Deletethis('comment', $where_c);

        exit(json_encode(array("info" => "删除成功", "status" => "y")));

    }
 	
    public function itemslist(){
    	$this->pagetext=array("商品管理","商品管理");
    	$id=$this->arg("id");
    	$where[]="`fid` ={$id}";
    	$ret=obj("api/Apidata")->Data_Select("items",$where,"`id` DESC ");
    	$this->ret=$ret;
    	$this->display();
    }

    public function additemslist(){

    	if(!IS_POST){

    		$this->display();
    		exit;
    	}else{
    		include CONFIG_PATH . 'siteconfig.php';
    		$data = obj('api/Api')->Form($this->POSTarg());
    		self::ChecktbkForm();
    		$data['model']=$Siteinfo['model'];
    		$data['fid']=$this->arg("fid");
    	    obj('api/ApiData')->Inserts('items', $data);
            echo json_encode(array("info" => "保存成功", "status" => "y","windows"=>"pop"));

    	}
    }

    public function deleteitemslist(){
    	error_reporting('0');
        $id=$this->arg("id");
        $where = " `id` ={$id}";
       obj('api/ApiData')->Deletethis('items', $where);
        exit(json_encode(array("info" => "删除成功", "status" => "y")));
    }

    public function editoritemslist(){
    	if(!IS_POST){
    		$id=$this->arg("id");
            $where[]="`id` ={$id}";
    	    $ret=obj("api/Apidata")->Data_Select("items",$where);
    	    $this->ret=$ret;
    	    $this->html='<input type="hidden" name="id" value="'.$ret['id'].'" />';
    	    $this->display('app/manage/view/items/additemslist');
    		exit;
    	}else{
    		include CONFIG_PATH . 'siteconfig.php';
    		$id=$this->arg("id");
    		$data = obj('api/Api')->Form($this->POSTarg());
    		self::ChecktbkForm();
    		$data['model']=$Siteinfo['model'];
    		$data['fid']=$this->arg("fid");
    		$where[]="  `id` ={$id} ";
            obj("api/Apidata")->Data_Updata("items",$data,$where);
            echo json_encode(array("info" => "保存成功", "status" => "y","windows"=>"pop"));

    	}

    }
	
	 public function mall()
    {
        $this->pagetext = array("商城管理", "管理商城");
        $where[] = "1";
        $baseurl = "index.php?r=manage/items/mall";
        $Page = obj('api/ApiData')->page("50", "mall", $where, "`id` DESC", $baseurl);
        $this->Page = $Page;
        $this->display();
    }
    public function addmall()
    {
        if (!IS_POST) {
            $this->pagetext = array("商城管理", "添加商城");
            $this->union=self::loadunion();
            $this->display();
            exit;
        } else {
            self::CheckmallForm();
            $data = obj('api/Api')->Form($this->POSTarg());
            obj('api/ApiData')->Inserts('mall', $data);
            echo json_encode(array("info" => "保存成功", "status" => "y"));
        }
    }
    public function editormall()
    {
        if (!IS_POST) {
            $this->pagetext = array("商城管理", "编辑商城");
            $id = $this->arg("id");
            $where[] = "`id` ={$id}";
            $ret = obj("api/Apidata")->Data_Select("mall", $where);
            $this->ret = $ret;
            $this->html = '<input type="hidden" name="id" value="' . $ret['id'] . '" />';
            $this->union=self::loadunion();
            $this->display('app/manage/view/items/addmall');
            exit;
        } else {
            self::CheckmallForm();
            $id = $this->arg("id");
            $where[] = "  `id` ={$id} ";
            $data = obj('api/Api')->Form($this->POSTarg());
            obj("api/Apidata")->Data_Updata("mall", $data, $where);
            echo json_encode(array("info" => "保存成功", "status" => "y"));
        }
    }
    public function deletemall()
    {
        error_reporting('0');
        $id = $this->arg("id");
        // $where[] = "`groupid` ={$id}";
        //查询该分类下面有没有文章
        //  $count = obj("api/ApiData")->Data_Count("forum", $where);
        // if($count>0){
        //     exit(json_encode(array("info" => "请先删除改分类下的文章", "status" => "n")));
        // }
        //删除
        $where = "  `id` ={$id}";
        obj('api/ApiData')->Deletethis('mall', $where);
        exit(json_encode(array("info" => "删除成功", "status" => "y")));
    }


    public function ItemsGetInfo(){
    	   header("Content-type: text/html; charset=utf-8"); 
           $tbk= new \ZhiCms\ext\Tbk;
    	   include CONFIG_PATH . 'siteconfig.php';
    	  
    	   $model=$Siteinfo['model'];
    	   $itemsurl=$_POST['url'];

          // $itemsurl="https://detail.tmall.com/item.htm?id=563685072391";
    	   //通用模式
    	   
			   preg_match('/[1-9]\d*/', $itemsurl,$itemsid);
    	    $info=$tbk->getinfo($Siteinfo['appkey'],$Siteinfo['secretKey'],$itemsid['0']);
			
    	   	

    	   	$items_data['title']=$info['title'];
			$items_data['itemiid']=$info['num_iid'];
    	   	$items_data['pic']=$info['pict_url'];
    	   	$items_data['itemsurl']=$info['item_url'];
    	   	$items_data['zkPrice']=$info['zk_final_price'];
    	   	$items_data['tkCommFee']=$info['reserve_price'];
    	   	$items_data['model']=$model;

    	   
    	   

    	   echo json_encode($items_data);
    } 

    public function duomai(){
		include CONFIG_PATH . 'apiset.php';
        header('Content-Type:text/html; charset=utf-8');
       //1.获取xml数据 
     $xmldata=file_get_contents("https://www.duomai.com/api/ads.php?hash=".$api['hash']."&action=getApplyAds");
     $xmlstring = simplexml_load_string($xmldata, 'SimpleXMLElement', LIBXML_NOCDATA);
     $value_array = json_decode(json_encode($xmlstring),true); 
	 $data =json_encode($value_array, JSON_UNESCAPED_UNICODE);
    $json = json_decode($data, true);//再次解析参数获得数组
     $result=array_shift($json);//减去第一组无用数组
    $ret=obj("api/Api")->object_array($json);
	 $a = obj('api/ApiData')->thisquery('BEGIN');
     for($key='0';$key<=100000;$key++){
		// $where[]="`ads_id` ='{$ret['fix_'.$key]['ads_id']}'";
	 //$chongfu=obj("api/Apidata")->Data_Select("duomai",$where);
	//if(!$chongfu){
		$shuju['ads_id']=$ret['fix_'.$key]['ads_id'];
		$shuju['ads_name']=$ret['fix_'.$key]['ads_name'];
		$shuju['channel']=$ret['fix_'.$key]['channel'];
		$shuju['status']=$ret['fix_'.$key]['status'];
		$shuju['applay_mode']=$ret['fix_'.$key]['applay_mode'];
		$shuju['hide']=$ret['fix_'.$key]['hide'];
		$shuju['type']=$ret['fix_'.$key]['type'];
		$shuju['cate_name']=$ret['fix_'.$key]['cate_name']=="" || $ret['fix_'.$key]['cate_name']==null ?"":$ret['fix_'.$key]['cate_name'];
		$shuju['ads_endtime']=$ret['fix_'.$key]['ads_endtime'];
		$shuju['ads_commission']=$ret['fix_'.$key]['ads_commission'];
		$shuju['site_url']=$ret['fix_'.$key]['site_url'];
		$shuju['site_logo']=$ret['fix_'.$key]['site_logo'];
		$shuju['site_description']=$ret['fix_'.$key]['site_description']=="" || $ret['fix_'.$key]['site_description']==null ? "":$ret['fix_'.$key]['site_description'];
		$shuju['adser']=$ret['fix_'.$key]['adser'];
		$shuju['country']=$ret['fix_'.$key]['country'];
		 obj("api/Apidata")->Inserts("duomai",$shuju);
	 //}
if($key%100==0){
    //每1w条提交一次
    $d = obj('api/ApiData')->thisquery('COMMIT');
    $e = obj('api/ApiData')->thisquery('BEGIN');
}
 echo 'ok';
    }
	$f = obj('api/ApiData')->thisquery('COMMIT');


    	exit(json_encode($ret, JSON_UNESCAPED_UNICODE));
    } 
	
	public function duomailist(){
        header('Content-Type:text/html; charset=utf-8');
       //1.获取xml数据 
     $xmldata=file_get_contents("https://www.duomai.com/api/ads.php?hash=355d9878e6469953b0383452533b6f43&action=getHighProductions");
     $xmlstring = simplexml_load_string($xmldata, 'SimpleXMLElement', LIBXML_NOCDATA);
     $value_array = json_decode(json_encode($xmlstring),true); 
	 $data =json_encode($value_array, JSON_UNESCAPED_UNICODE);
    $json = json_decode($data, true);//再次解析参数获得数组
     //$result=array_shift($json);//减去第一组无用数组
    //$ret=obj("api/Api")->object_array($json);



    	exit(strip_tags(json_encode($json, JSON_UNESCAPED_UNICODE)));
    } 
	


    public function type(){
        $this->pagetext=array("分类管理","管理专辑分类");
        $where[] = "1";
        $baseurl = "index.php?r=manage/items/type";
        $Page = obj('api/ApiData')->page("50", "group", $where, "`id` DESC", $baseurl);
        $this->Page = $Page;
        $this->display();
    }

    public function addtype(){
      if(!IS_POST){
            $this->pagetext=array("分类管理","新建分类");
            $this->display();
            exit;
        }else{
             self::CheckTypeForm();
             $data = obj('api/Api')->Form($this->POSTarg());
             obj('api/ApiData')->Inserts('group', $data);
             echo json_encode(array("info" => "保存成功", "status" => "y"));

        }  
    }
    
    public function editortype(){
     if(!IS_POST){
            $this->pagetext=array("分类管理","编辑分类");
            $id=$this->arg("id");
            $where[]="`id` ={$id}";
            $ret=obj("api/Apidata")->Data_Select("group",$where);
            $this->ret=$ret;
            $this->html='<input type="hidden" name="id" value="'.$ret['id'].'" />';
            $this->display('app/manage/view/items/addtype');
            exit;
        }else{
             self::CheckTypeForm();
             $id=$this->arg("id");
             $where[]="  `id` ={$id} ";
             $data = obj('api/Api')->Form($this->POSTarg());
             obj("api/Apidata")->Data_Updata("group",$data,$where);
             echo json_encode(array("info" => "保存成功", "status" => "y"));
        }

    }

    public function deletetype(){
        error_reporting('0');
        $id=$this->arg("id");
        $where[] = "`groupid` ={$id}";
        //查询该分类下面有没有文章
        $count = obj("api/ApiData")->Data_Count("forum", $where);
        if($count>0){
            exit(json_encode(array("info" => "请先删除改分类下的文章", "status" => "n")));
        }

        //删除
        $where="  `id` ={$id}";
        obj('api/ApiData')->Deletethis('group', $where);
        exit(json_encode(array("info" => "删除成功", "status" => "y")));



    }
	 public  function union(){
        $this->pagetext = array("联盟模型", "联盟模型");
        $where[] = "1";
        $baseurl = "index.php?r=manage/items/union";
        $Page = obj('api/ApiData')->page("50", "union", $where, "`id` DESC", $baseurl);
        $this->Page = $Page;
        $this->display();
    }

    public function addunion(){
     if (!IS_POST) {
            $this->pagetext = array("联盟模型", "新建联盟模型");
            $this->display();
            exit;
        } else {
            //self::CheckmallForm();
            $data = obj('api/Api')->Form($this->POSTarg());
            $data['code']=base64_encode($_POST['code']);
            obj('api/ApiData')->Inserts('union', $data);
            echo json_encode(array("info" => "保存成功", "status" => "y"));
        }
    }

     public function editorunion()
    {
        if (!IS_POST) {
            $this->pagetext = array("联盟模型", "编辑联盟模型");
            $id = $this->arg("id");
            $where[] = "`id` ={$id}";
            $ret = obj("api/Apidata")->Data_Select("union", $where);
            $this->ret = $ret;
            $this->html = '<input type="hidden" name="id" value="' . $ret['id'] . '" />';
            $this->display('app/manage/view/items/addunion');
            exit;
        } else {
           // self::CheckmallForm();
            $id = $this->arg("id");
            $where[] = "  `id` ={$id} ";
            $data = obj('api/Api')->Form($this->POSTarg());
            $data['code']=base64_encode($_POST['code']);
            obj("api/Apidata")->Data_Updata("union", $data, $where);
            echo json_encode(array("info" => "保存成功", "status" => "y"));
        }
    }

    public function deleteunion(){
        error_reporting('0');
        $id=$this->arg("id");
        $where = "  `id` ={$id}";
        obj('api/ApiData')->Deletethis('union', $where);
        exit(json_encode(array("info" => "删除成功", "status" => "y")));
    }


    public function navcount($id){
       
            $where[] = "`groupid` ={$id}";
            $count = obj("api/ApiData")->Data_Count("forum", $where);
            echo $count;
        
    }


    public function CheckTypeForm(){

        if(!$this->arg("groupname")){
            exit(json_encode(array("info" => "请填写分类名称", "status" => "n")));
        }
         if(!$this->arg("px")){
            exit(json_encode(array("info" => "请填写排序", "status" => "n")));
        }

    }

 	public function CheckItemsForm($title,$type,$keywords,$dec,$pic,$uid){
 		 if(!$title){
    		 	exit(json_encode(array("info" => "请填写标题", "status" => "n")));
    		 }
    		  if($type=='0'){
    		 	exit(json_encode(array("info" => "请选择分类", "status" => "n")));
    		 }
    		 if(!$keywords){
    		 	exit(json_encode(array("info" => "请输入关键字", "status" => "n")));
    		 }
    		  if(!$dec){
    		 	exit(json_encode(array("info" => "请输入描述", "status" => "n")));
    		 }
    		 if(!$pic){
    		 	exit(json_encode(array("info" => "请上传封面", "status" => "n")));
    		 }
    		 if($uid=='0'){
    		 	exit(json_encode(array("info" => "请选择一个马甲", "status" => "n")));
    		 }
 	}  

 	public function ChecktbkForm(){
 		if(!$this->arg("url")){
 			exit(json_encode(array("info" => "请输入宝贝网址", "status" => "n")));
 		}
 		if(!$this->arg('itemsurl')){
 			exit(json_encode(array("info" => "请输入宝贝链接", "status" => "n")));
 		}
 		if(!$this->arg('wa')){
 			exit(json_encode(array("info" => "请输入文案", "status" => "n")));
 		}
 		if(!$this->arg('taoToken')){
 			exit(json_encode(array("info" => "请输入宝贝口令", "status" => "n")));
 		}
 	}
	 //加载联盟
    public function loadunion(){

        $where[] = "1";
        $ret = obj("api/Apidata")->Data_Select("union", $where,"`id` DESC");
        return $ret;

    }
}